<template>
  <div class="login">
    <div class="form-box" v-loading="loading" element-loading-background="rgba(0, 0, 0, 0)">
      <div class="title">
        <h1>智远后台管理</h1>
      </div>
      <el-form
        :model="account"
        status-icon
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名">
          <el-input v-model="account.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="account.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
  
<script>
import { reactive, ref, onMounted } from "vue";
//   import { useStore } from 'vuex'
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { userLogin } from "@/api/PC-Home.js";
export default {
  setup() {
    const loading = ref(false);
    //   const store = useStore()
    const router = useRouter();
    const account = reactive({
      username: "",
      password: "",
      captcha: ""
    });
    /**
     * 根据账号密码登录，拿到 token 并储存
     */
    const login = async () => {
      const { username, password } = account;
      userLogin({ userName: username, password: password }).then(res => {
        if (res.code === 200) {
          ElMessage.success("登录成功");
          sessionStorage.setItem("isLogin", res.data);
          router.push("/admin");
        } else {
          ElMessage.error(res.message);
        }
      });
      // try {
      //   loading.value = true
      //   loading.value = false
      //   router.push("/home")
      //   ElMessage({
      //     message: '登录成功',
      //     type: 'success',
      //   })
      // } catch (e) {
      //   loading.value = false
      // }
    };
    /**
     * 节流登录
     */
    onMounted(() => {});

    return {
      account,
      loading,
      login
    };
  }
};
</script>
  
<style lang="scss">
.login {
  width: 100%;
  height: 100%;
  background: #1b2c5f url("../../assets/img/index-model-comprehensive.png")
    no-repeat center center;
  background-size: cover;

  .team-name {
    position: fixed;
    left: 40px;
    top: 50%;
    width: 50px;
    transform: translateY(-50%);
  }

  .form-box {
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 445px;

    .title {
      height: 37px;
      font-size: 30px;
      line-height: 37px;
      margin-bottom: 15%;

      h1 {
        padding-left: 74px;
        box-sizing: border-box;
        text-align: left;
        color: #8c98ae;
      }
    }
  }
}
</style>
  